package cn.edu.ren.controller;

import cn.edu.ren.entity.Item;
import cn.edu.ren.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: cn.edu.ren.controller
 * @ClassName: ElasticsearchController
 * @Description:
 * @author: RZS
 * @date: 2021/7/7  22:37
 */
@RestController
@RequestMapping("/es")
public class ElasticsearchController {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private ItemService itemService;

    // 排序
    @GetMapping("/sort_search")
    public List<Item> searchAndSort() {
        return itemService.searchAndSort();
    }

    // rangeQuery   范围匹配
    @GetMapping("/range_query")
    public List<Item> rangeQuery(){
        return itemService.rangeQuery();
    }

    // booleanQuery  布尔查询查询
    @GetMapping("/boolean_query")
    public List<Item> booleanQuery(){
        return itemService.booleanQuery();
    }

    // fuzzyQuery  通配符匹配
    @GetMapping("/fuzzy_query")
    public List<Item> fuzzyQuery(){
        return itemService.fuzzyQuery();
    }

    // 词条匹配，不分词
    @GetMapping("/term_query")
    public List<Item> termQuery(){
        return itemService.termQuery();
    }

    // matchQuery：词条匹配，先分词然后在调用termQuery进行匹配
    @GetMapping("/match_query")
    public List<Item> matchQuery(){
        return itemService.matchQuery();
    }

    @GetMapping("/create")
    public String createIndex(){
        if(elasticsearchTemplate == null){
            return "创建索引失败ES为null";
        }
        elasticsearchTemplate.createIndex(Item.class);
        return "创建索引成功";
    }

    @GetMapping("/save")
    public String save(){
        // 封装item对象
        Item item = new Item();
        item.setId(1L);
        item.setTitle("java api方式");
        item.setBrand("javaES");
        item.setPrice(1980.0);
        item.setCategory("java");
        item.setImages("https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB.svg");

        itemService.save(item);
        return "新增数据成功";
    }

    @GetMapping("/batch-save")
    public String batchSave(){
        List<Item> itemList = new ArrayList<>();
        for (int i = 143; i < 252; i++) {
            // 封装item对象
            Item item = new Item();
            item.setId(i * 1L);
            item.setTitle(i+"红米"+i);
            item.setBrand("手机");
            item.setPrice(2980.0 + i);
            item.setCategory("手机");
            item.setImages("https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB"+i+".svg");
            // 存入集合
            itemList.add(item);
        }
        itemService.batchSave(itemList);
        return "新增数据成功";
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable Integer id){
        Item item = new Item();
        item.setId(id*1L);
        // 修改title
        item.setTitle(id+"这是被修改的"+id);

        itemService.save(item);
        return "修改成功";
    }

    @GetMapping("/find_all")
    public List<Item> findAll(){
        System.err.println("总数："+itemService.findAll().size());
        return itemService.findAll();
    }

    @GetMapping("/find_by_id")
    public Item findById(){
        return itemService.findById(1L);
    }

    @GetMapping("/find_by_ids")
    public List<Item> findByIds(){
        List<Long> ids = new ArrayList<>();
        ids.add(2L);
        ids.add(5L);
        ids.add(8L);
        return itemService.findByIds(ids);
    }

    @GetMapping("/find_all_page")
    public List<Item> findAllPage(){
        // 构建分页对象
        Pageable pageable = PageRequest.of(5, 3);
        return itemService.findAll(pageable);
    }

    @GetMapping("/sort")
    public List<Item> findAllSort(){
        // 构建排序对象
        Sort sort = Sort.by("price") // 对哪一个域进行排序
                    .descending();// 排序规则
        return itemService.findAll(sort);
    }

}
